/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EXtremecode.PresentationLayer;

public partial class ASCXNumberBox : System.Web.UI.UserControl,IASCXControl
{
    private bool isSetDisplayMethodCalled = false;
    
    private int decimalPlaces = 2;
    public int DecimalPlaces
    {
        get { return decimalPlaces; }
        set { decimalPlaces = value; }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    public override string ClientID
    {
        get
        {
            return CurrentClientId;
        }
    }
    
    protected void Page_PreRender(object sender, EventArgs e)
    {
        vldrRequired.ErrorMessage = string.Format("{0} : {1}",
            caption, ResourceProvider.GetGeneralResourceString("Message_FieldRequired"));


        string expression = "Expression_Integer"; //integer
        string errorMessage = "Message_InvalidInteger";
        if (isDecimalNumber)
        {
            errorMessage = "Message_InvalidNumber";
            expression = "Expression_Number"; 
        }


        vldrRegx.ErrorMessage = string.Format("{0} : {1}",
            caption, ResourceProvider.GetGeneralResourceString(errorMessage));
        vldrRegx.ValidationExpression = ResourceProvider.GetExpression(expression);



        //Script block
        string currentClientId = this.CurrentClientId;
        string originalClientId = this.OriginalClientId;
        string inputClientId = this.InputClientId;
        string displayClientId = this.DisplayClientId;

        
        StringBuilder sbScriptBlock = new StringBuilder();

        sbScriptBlock.AppendLine("<script>");

        sbScriptBlock.AppendLine(
            string.Format("var {0} = document.getElementById('{0}');", currentClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.OriginalCtrl = document.getElementById('{1}');",
                currentClientId, originalClientId));

        if (!IsReadOnly)
        {
            sbScriptBlock.AppendLine(
                string.Format("var {0} = document.getElementById('{0}');", inputClientId));

            sbScriptBlock.AppendLine(
               string.Format("{0}.CurrentCtrl = document.getElementById('{1}');",
                   inputClientId, currentClientId));
        }

        sbScriptBlock.AppendLine(
            string.Format("{0}.InputCtrl = document.getElementById('{1}');",
            currentClientId, inputClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.DisplayCtrl = document.getElementById('{1}');",
            currentClientId, displayClientId));

        sbScriptBlock.AppendLine(
           string.Format("{0}.RateCtrl = document.getElementById('{1}');",
           currentClientId, hdnConversionRate.ClientID));

        sbScriptBlock.AppendLine(
            string.Format("{0}.SetConversionRate = setConversionRate;", currentClientId));

        
        //script block . initalizing of number format object.
        sbScriptBlock.AppendLine(
            string.Format("{0}.FormattingObject = new NumberFormat();", currentClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.FormattingObject.setCommas(true);", currentClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.FormattingObject.setPlaces({1});"
            ,currentClientId,decimalPlaces));

        sbScriptBlock.AppendLine(
            string.Format("{0}.FormattingObject.setCurrency(false);", currentClientId));


        
        
        sbScriptBlock.AppendLine(
            string.Format("{0}.SetValue = SetTextValue;", currentClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.GetValue = GetTextValue;", currentClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.ResetValue = resetValue;", currentClientId));


        sbScriptBlock.AppendLine("</script>");

        scriptBlockLiteral.Text = sbScriptBlock.ToString();

        if (!isSetDisplayMethodCalled)
        {
            SetDisplay();
        }
    }

    private void SetDisplay()
    {
        if (IsReadOnly)
        {
            lblNumber.Text = Text;

        }
    }

    public int MaxLength
    {
        get
        {
            return txtNumber.MaxLength;
        }
        set
        {
            txtNumber.MaxLength = value;
        }
    }

    public TextBoxMode TextMode
    {
        get
        {
            return txtNumber.TextMode;
        }
        set
        {
            txtNumber.TextMode = value;
        }
    }

    public decimal ConversionRate
    {
        get
        {
            decimal rate;
            if (!decimal.TryParse(hdnConversionRate.Value, out rate))
            {
                rate = 1;
            }
            return rate;
        }
    }

    private bool isDecimalNumber = true;
    public bool IsDecimalNumber
    {
        get { return isDecimalNumber; }
        set
        { 
            isDecimalNumber = value;
            if (value)
            {
                decimalPlaces = 2;
            }
            else
            {
                decimalPlaces = 0; 
            }
        }
    }

    public decimal? OriginalValue
    {
        get
        {
            decimal? val = null;
            decimal dec = 0;
            if (decimal.TryParse(hdnOriginal.Value, out dec))
            {
                val = dec;
            }

            return val;
        }
    }

    public decimal? Value
    {
        get
        {
            decimal? val = null;
            if (!isReadOnly || !string.IsNullOrEmpty(txtNumber.Text))
            {
                string strNumber = txtNumber.Text.Trim();
                strNumber = strNumber.Replace(",", "");
                if (strNumber != string.Empty)
                {
                    decimal dec = 0;
                    if (decimal.TryParse(strNumber, out dec))
                    {
                        val = dec / ConversionRate;
                    }

                }
            }
            else
            {
                decimal dec = 0;
                if (decimal.TryParse(hdnCurrent.Value, out dec))
                {
                    val = dec;
                }
            }
            return val;
        }
        set
        {
            txtNumber.Text = string.Empty;
            if (value != null)
            {
                decimal val = Convert.ToDecimal(value);
                decimal dec = val * ConversionRate; 
                hdnCurrent.Value = hdnOriginal.Value = val.ToString();
                lblNumber.Text = txtNumber.Text = (isDecimalNumber) ? dec.ToString("N") :
                    dec.ToString("N0");

                //for avoiding SetDisply method to execute.
                SetDisplay();
                isSetDisplayMethodCalled = true;
            }
        }
    }

    


    #region IASCXControl Members

    public string DisplayCssClass
    {
        get
        {
            return lblNumber.CssClass;
        }
        set
        {
            lblNumber.CssClass = value;
        }
    }
    
    private bool isReadOnly = false;
    public bool IsReadOnly
    {
        get
        {
            return isReadOnly;
        }
        set
        {
            
            isReadOnly = value;
            if (isReadOnly)
            {
                multiView.SetActiveView(viewReadOnly);
            }
            else
            {
                multiView.SetActiveView(viewEditable);
            }
        }
    }
    
    object IASCXControl.Value
    {
        get
        {
            return Value;  
        }
        set
        {
            Value = value as decimal?;
        }
    }
    object IASCXControl.OriginalValue
    {
        get
        {
            return OriginalValue;
        }
        
    }
    public bool HasValueChanged
    {
        get
        {
            return (Value != OriginalValue);
        }
    }


    public string Text
    {
        get
        {
            decimal? val = Value;
            string str = string.Empty;
            if (val != null)
            {
                  decimal dec = val.Value  * ConversionRate;
                    str = (isDecimalNumber) ? dec.ToString("N") :
                        dec.ToString("N0");
                
            }
            return str;
        }
    }

    public string InputClientId
    {
        get
        {
            return txtNumber.ClientID;
        }
    }
    public string OriginalClientId
    {
        get
        {
            return hdnOriginal.ClientID;
        }
    }
    public string CurrentClientId
    {
        get
        {
            return hdnCurrent.ClientID;
        }
    }
    public string InputUniqueId
    {
        get
        {
            return txtNumber.UniqueID;
        }
    }
    public string DisplayClientId
    {
        get
        {
            return lblNumber.ClientID;
        }
    }
    public string DisplayUniqueId
    {
        get
        {
            return lblNumber.UniqueID;
        }
    }

    public Control InputControl
    {
        get
        {
            return txtNumber;
        }
    }

    public string JSResetFunction
    {
        get
        {
            return "ResetTextBox";
        }
    }

    private bool isRequired = false;
    public bool IsRequired
    {
        get
        {
            return isRequired;
        }
        set
        {
            isRequired = value;

            if (isRequired)
            {
                txtNumber.CssClass = "requiredField";
                vldrRequired.Enabled = true;
                vldrRequired.Display = ValidatorDisplay.Dynamic;
            }
            else
            {
                txtNumber.CssClass = "";
                vldrRequired.Enabled = false;
                vldrRequired.Display = ValidatorDisplay.None;
            }
        }
    }

    private string caption = string.Empty;
    public string Caption
    {
        get
        {
            return caption;
        }
        set
        {
            caption = value;
        }
    }

    #endregion
}
